<template>
  <el-dialog v-model="visibleTsl" :before-close="beforeCloseTsl" title="选择设备" width="600">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-select v-model="form.deviceName">
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.deviceName"
            :value="item.deviceName"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="handleClose">取 消</el-button>
    <el-button type="primary" @click="handleConfirm">确 定</el-button>
  </span>
  </el-dialog>
</template>
<script>
import { select,updateApi } from '@/api/iot/device';
export default {
  name: 'device-modal',
  data() {
    return {
      visibleTsl: false,
      list: [],
      row: {},
      form: {
        deviceName: '',
      },
      rules: {
        deviceName: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
      },
    };
  },
  methods: {
    handleClose(){
      this.visibleTsl = false;
      this.$refs.ruleForm.resetFields();
    },
    handleConfirm(){
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          console.log(this.row);
          const data = {
            deviceName:this.form.deviceName,
            productKey:this.row.productId,
            packageVersion:this.row.packageVersion,
            packageUrl:this.row.packageUrl,

          }
          updateApi(data).then(res=>{
            this.$message({
              message: '操作成功',
              type: 'success'
            });
          })
          this.handleClose();
        } else {
          return false;
        }
      });
    },
    getData(row) {
      select(row.productId).then(res => {
        console.log(res);
        this.list = res.data.data;
      });
    },
    showModal(row) {
      this.row = row;
      this.visibleTsl = true;
      this.getData(row);
    },
  },
};
</script>
